package com.cyan.note.mapper;

import com.cyan.note.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 用户数据访问层
 * 
 * @author ZhangzhenYu
 * @since 2025-01-14
 */
@Mapper
public interface UserMapper {
    
    /**
     * 根据用户名查找用户
     */
    @Select("SELECT id, username, password, name as real_name, phone, email, avatar, " +
            "user_type as role, shop_id, status, last_login_time, last_login_ip, " +
            "created_at, updated_at " +
            "FROM users WHERE username = #{username}")
    User findByUsername(String username);
    
    /**
     * 根据ID查找用户
     */
    @Select("SELECT id, username, password, name as real_name, phone, email, avatar, " +
            "user_type as role, shop_id, status, last_login_time, last_login_ip, " +
            "created_at, updated_at " +
            "FROM users WHERE id = #{userId}")
    User selectById(Long userId);

    // ===== 以下为超级管理员专用的店长管理方法 =====

    /**
     * 查询所有店长账户（仅系统信息）
     */
    @Select("SELECT id, username, name as real_name, phone, email, " +
            "user_type as role, shop_id, status, last_login_time, last_login_ip, " +
            "created_at, updated_at " +
            "FROM users WHERE user_type = 'manager' ORDER BY created_at DESC")
    List<User> selectAllManagers();

    /**
     * 根据店铺ID查询店长账户
     */
    @Select("SELECT id, username, name as real_name, phone, email, " +
            "user_type as role, shop_id, status, last_login_time, last_login_ip, " +
            "created_at, updated_at " +
            "FROM users WHERE user_type = 'manager' AND shop_id = #{shopId}")
    List<User> selectManagersByShopId(Long shopId);

    /**
     * 根据状态查询店长账户
     */
    @Select("SELECT id, username, name as real_name, phone, email, " +
            "user_type as role, shop_id, status, last_login_time, last_login_ip, " +
            "created_at, updated_at " +
            "FROM users WHERE user_type = 'manager' AND status = #{status} ORDER BY created_at DESC")
    List<User> selectManagersByStatus(String status);

    /**
     * 创建新用户（店长）
     */
    @Insert("INSERT INTO users (username, password, name, phone, email, user_type, shop_id, status) " +
            "VALUES (#{username}, #{password}, #{realName}, #{phone}, #{email}, #{role}, #{shopId}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    /**
     * 更新用户基本信息（仅系统信息）
     */
    @Update("UPDATE users SET name = #{realName}, phone = #{phone}, email = #{email}, " +
            "updated_at = CURRENT_TIMESTAMP WHERE id = #{id}")
    int updateBasicInfo(User user);

    /**
     * 更新用户状态
     */
    @Update("UPDATE users SET status = #{status}, updated_at = CURRENT_TIMESTAMP WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") String status);

    /**
     * 更新用户密码
     */
    @Update("UPDATE users SET password = #{password}, updated_at = CURRENT_TIMESTAMP WHERE id = #{id}")
    int updatePassword(@Param("id") Long id, @Param("password") String password);

    /**
     * 删除用户
     */
    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 检查用户名是否存在
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    long countByUsername(String username);

    /**
     * 检查用户名是否存在（排除指定用户）
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username} AND id != #{excludeId}")
    long countByUsernameExcludeId(@Param("username") String username, @Param("excludeId") Long excludeId);

    /**
     * 统计店长总数（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM users WHERE user_type = 'manager'")
    long countManagers();

    /**
     * 按状态统计店长数量（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM users WHERE user_type = 'manager' AND status = #{status}")
    long countManagersByStatus(String status);

    // ===== 以下为系统监控专用统计方法 =====

    /**
     * 统计用户总数（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM users")
    long countTotalUsers();

    /**
     * 按角色统计用户数量（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM users WHERE user_type = #{role}")
    long countUsersByRole(String role);

    /**
     * 按状态统计用户数量（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM users WHERE status = #{status}")
    long countUsersByStatus(String status);
}